
//
//  V4HomeLeftView.m
//  XinKaiFa55like
//
//  Created by 55like on 23/09/2017.
//  Copyright © 2017 55like lj. All rights reserved.
//

#import "V4HomeLeftView.h"
#import "RHTableView.h"
#import "V4LicensePlateViewController.h"
@interface V4HomeLeftView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)RHTableView*mtableView;
@property(nonatomic,weak)UIView*contentView;
@property(nonatomic,strong)NSDictionary*currentdic;
@end

@implementation V4HomeLeftView


+(void)showMeWithCallBackBlock:(AllcallBlock) callBackBlock{
    if (![UTILITY userTypeData]) {
        return;
    }
    V4HomeLeftView*me=[UTILITY.currentViewController getAddValueForKey:@"V4HomeLeftView"];
    if (me==nil) {
        me=[[V4HomeLeftView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        [me addView];
        [UTILITY.currentViewController setAddValue:me forKey:@"V4HomeLeftView"];
        
    }
    me.hidden=NO;
    me.callBackBlock=callBackBlock;
    [UTILITY.currentViewController.view addSubview:me];

}


#pragma mark -  写UI
-(void)addView{
     WSSizeButton*btnclose=[RHMethods buttonWithframe:CGRectMake(0, 0, kScreenWidth, kScreenHeight) backgroundColor:RGBACOLOR(127, 127, 127, 0.5) text:@"" font:0   textColor:nil radius:0 superview:self];
    [btnclose addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView*viewContent=[RHMethods viewWithFrame:CGRectMake(0, 0,200* kScreenWidth/360.0, kScreenHeight) backgroundcolor:zrgbwhiteColor superView:self];
    _contentView=viewContent;
    {
    UIView*viewHeader=[RHMethods viewWithFrame:CGRectMake(0, 0, viewContent.frameWidth, 64) backgroundcolor:rgb(48, 49, 53) superView:_contentView];
       [RHMethods imageviewWithFrame:CGRectMake(10, 36, 15, 12) defaultimage:@"xzicon1" supView:viewHeader];
        [RHMethods lableX:30 Y:34 W:0 Height:15 font:15 superview:viewHeader withColor:zrgbwhiteColor text:@"管理分组"];
        
    
    }
    
    {
    UIView*viewtopAll=[RHMethods viewWithFrame:CGRectMake(0, 64, viewContent.frameWidth, 40) backgroundcolor:nil superView:viewContent];
        [RHMethods imageviewWithFrame:CGRectMake(10, 15, 15, 10) defaultimage:@"xkf_all" supView:viewtopAll];
        [RHMethods lableX:30 Y:13 W:0 Height:14 font:14 superview:viewtopAll withColor:nil text:[NSString stringWithFormat:@"全部(%lu)",(unsigned long)[[[UTILITY userTypeData] objectForJSONKey:@"bigcate"] count]]];
    }
    
    
    _mtableView =[[RHTableView alloc]initWithFrame:CGRectMake(0, 104, viewContent.frameWidth, kScreenHeight-104) style:UITableViewStylePlain];
    
    _mtableView.delegate = self;
    _mtableView.dataSource = self;
    _mtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [viewContent addSubview:_mtableView];
    
    
    if(UTILITY.homeLeftArray==nil){
        NSMutableArray*marray=[NSMutableArray new];
        NSArray*array=[[UTILITY userTypeData] objectForJSONKey:@"bigcate"];
        for (NSDictionary*darry in array) {
            NSMutableDictionary*mdic=[NSMutableDictionary dictionaryWithDictionary:darry];
            [marray addObject:mdic];
        }
        UTILITY.homeLeftArray=marray;
    }
    
    [_mtableView reloadData];
    
}

-(void)didAddSubview:(UIView *)subview{

}
#pragma mark  tableview网络请求

#pragma mark - 按钮事件监听
-(void)close:(UIButton*)btn{
//    [self removeFromSuperview];
    self.hidden=YES;
    
    
    
}

#pragma mark - 代理事件



#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(RHTableView *)tableView{
    
    
    return UTILITY.homeLeftArray.count;
    
}

-(NSInteger)tableView:(RHTableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSMutableDictionary*subcate=UTILITY.homeLeftArray[section];
    if ([[subcate objectForKey:@"isopen"] isEqualToString:@"1"]) {
        
        return [subcate[@"list"] count];
    }else{
        return 0;
    }
    
}

-(CGFloat)tableView:(RHTableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}
-(CGFloat)tableView:(RHTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 35;
}
-(CGFloat)tableView:(RHTableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    return 0.001;
}

-(UIView *)tableView:(RHTableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
     NSMutableDictionary*subcate=UTILITY.homeLeftArray[section];
    UIView*view1=[RHMethods viewWithFrame:CGRectMake(0, 0, _contentView.frameWidth, 40) backgroundcolor:zrgbwhiteColor superView:nil];
    [RHMethods lableX:30 Y:13 W:0 Height:14 font:14 superview:view1 withColor:nil text:[subcate objectForJSONKey:@"name"]];
   UIImageView*imagv= [RHMethods imageviewWithFrame:CGRectMake(151, 14, 8, 13) defaultimage:@"Righticon070116" supView:view1];
    CGPoint center=imagv.center;
    [RHMethods viewWithFrame:CGRectMake(10, 39, view1.frameWidth-10-32, 1) backgroundcolor:rgb(229,229,229) superView:view1];
    if ([[subcate objectForKey:@"isopen"] isEqualToString:@"1"]) {
        
        imagv.image=[UIImage imageNamed:@"xkf_xiala2"];
        imagv.frameWidth=13;
        imagv.frameHeight=8;
        imagv.center=center;
    }
    [view1 addViewClickBlock:^(UIView *view) {
        
        if ([[subcate objectForKey:@"isopen"] isEqualToString:@"1"]) {
            [subcate setObject:@"0" forKey:@"isopen"];
        }else{
            [subcate setObject:@"1" forKey:@"isopen"];
        }
        [tableView reloadData];
    }];
    
    return view1;
    
    
}

#pragma mark  cell处理
- (UITableViewCell *)tableView:(RHTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView ==_mtableView){
        //
        UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            cell.frameWidth=_contentView.frameWidth;
            
            UILabel*lbtitle=[RHMethods lableX:35 Y:0 W:180 Height:35 font:12 superview:cell withColor:nil text:@"名称"];
            
            UIImageView*imgV=[RHMethods imageviewWithFrame:CGRectMake(0, 0, 12, 8) defaultimage:@"xkf_ture" supView:cell];
            imgV.centerY=lbtitle.centerY;
            imgV.frameRX=10;
            
            [cell setAddValue:lbtitle forKey:@"lbtitle"];
            [cell setAddValue:imgV forKey:@"imgV"];
            
        }
        UILabel*lbtitle=[cell getAddValueForKey:@"lbtitle"];
        UILabel*imgV=[cell getAddValueForKey:@"imgV"];
        NSMutableDictionary*subcate=UTILITY.homeLeftArray[indexPath.section];
            
          NSDictionary*dic=  subcate[@"list"][indexPath.row] ;
        lbtitle.text=[dic objectForJSONKey:@"name"];
        imgV.hidden=YES;
        if (dic==_currentdic) {
            imgV.hidden=NO;
        }
        
        return cell;
        
    }
    return [UITableViewCell new];
}
#pragma mark  选中cell
-(void)tableView:(RHTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSMutableDictionary*subcate=UTILITY.homeLeftArray[indexPath.section];
    
    NSDictionary*dic=  subcate[@"list"][indexPath.row] ;
    
    _currentdic=dic;
    [tableView reloadData];
    if (self.callBackBlock) {
        self.callBackBlock(dic,200,nil);
        [self close:nil];
    }
    
//      [UTILITY.currentViewController pushController:[V4LicensePlateViewController class] withInfo:@"type" withTitle:[dic valueForJSONStrKey:@"name"] withOther:dic];

}
//-didsel
@end

